package Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Dao.StudentsInformationDao;
import Entity.Student;

/**
 * Servlet implementation class StudentPersonalServlet
 */
//@WebServlet("/StudentPersonalServlet")
public class StudentPersonalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentPersonalServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//解决乱码
		response.setContentType("text/html;charset=utf-8");//解决乱码
		response.setCharacterEncoding("utf-8");
		/*
		 * PrintWriter out=response.getWriter(); //把编码设置放到输出流获取之前 out.print("xxxx");
		 */
		/*
		 * HttpSession session=request.getSession(); //通过当前session中的学号查询该学生的所有信息 String
		 * studentNo=(String)session.getAttribute("session_userName"); Student
		 * student=new StudentsInformationDao().queryStudentByNo(studentNo);
		 * session.setAttribute("session_studentPersonalInformation", student);
		 * //将查询结果放入session中 response.sendRedirect("myPersonalInformation_student.jsp");
		 */
		
		String opttype=request.getParameter("opttype");
		String studentNo = "", studentName, sex, dept, major, studentClass, studentId, pwd, tel;
		System.out.print(opttype);
		switch(opttype) {
		case "showMyPersonal":
			/*
			 * request.getSession().setAttribute("myPersonal_student", new
			 * StudentsInformationDao().queryStudentByNo(studentNo));
			 *//* request.getContextPath() + "/test.html" */
			//response.sendRedirect(request.getContextPath()+"myPersonalInformation_student.jsp");
			response.sendRedirect("JSP_studentPersonalManage/myPersonalInformation_student.jsp");
			//response.sendRedirect("firstPage.jsp");
			//response.sendRedirect("/rtccp/user/a.jsp");
			break;
		
		
		/*
		 * case "alterMyPersonal": studentNo=request.getParameter("studentNo"); Student
		 * student=new StudentsInformationDao().queryStudentByNo(studentNo);
		 * request.getSession().setAttribute("session_myPersonal_student", student);
		 * response.sendRedirect("alterMyPersonal_student.jsp"); break;
		 */
			
		case "update":
			studentNo = request.getParameter("studentNo");
			studentName = request.getParameter("studentName");
			sex = request.getParameter("sex");
			dept = request.getParameter("dept");
			major = request.getParameter("major");
			studentClass = request.getParameter("studentClass");
			studentId = request.getParameter("studentId");
			pwd = request.getParameter("pwd");
			tel = request.getParameter("tel");
			Student stu=new Student(studentNo,studentName,sex,dept,major,studentClass,studentId,pwd,tel);
			 if(new StudentsInformationDao().alterStudentInformation(stu)>0) {
				 System.out.println("111111111111111111111");
				 //out.print("<script type='text/javascript'>alert('修改成功')</script>");
				 //response.sendRedirect("showAllStudents.jsp");
				 response.sendRedirect("/UniversityResourceSystem/StudentPersonalServlet?opttype=showMyPersonal"); 
				/*
				 * response.sendRedirect(
				 * "JSP_studentPersonalManage/myPersonalInformation_student.jsp");
				 */
			 }
			 //response.sendRedirect("StudentPersonalServlet?opttype=showMyPersonal"); 
			 break;
		}
		/*
		 * out.flush(); out.close();
		 */
		
	}

}
